
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.TableColumnExt;
import org.jdesktop.swingx.decorator.*;


public class JXTableAsLineNumberTable {
    public static void main(String[] args) {
        final MyTableModel model = new MyTableModel();
        final JXTable table = new JXTable();
//        table.setAutoCreateRowSorter(true);
        table.setModel(model);
        JFrame frame = new JFrame("JXTable with rowHeader");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(table);
        LineNumberTable lineNumberTable = new LineNumberTable(table);
        scrollPane.setRowHeaderView(lineNumberTable);
        scrollPane.setCorner(JScrollPane.UPPER_LEADING_CORNER, lineNumberTable.getTableHeader());
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(scrollPane, BorderLayout.CENTER);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    static class MyTableModel extends AbstractTableModel {
        public int getRowCount() {
            return 50;
        }


        public int getColumnCount() {
            return 4;
        }

        public Object getValueAt(int arg0, int arg1) {
            if (arg1 == 0) {
                return arg0;
            }
            return arg0 + "-" + arg1;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Integer.class;
            }
            return super.getColumnClass(columnIndex);
        }


    }

    static class LineNumberTable extends JXTable {
        private JXTable mainTable;

        public LineNumberTable(JXTable table) {
            super();
            mainTable = table;
            setAutoCreateColumnsFromModel(false);
            setModel(mainTable.getModel());
            setSelectionModel(mainTable.getSelectionModel());
            setAutoscrolls(false);
            addColumn(new TableColumnExt(0));
            getColumnModel().getColumn(0).setCellRenderer(
                    new JTableHeader().getDefaultRenderer());
            getColumnModel().getColumn(0).setPreferredWidth(50);
            setPreferredScrollableViewportSize(getPreferredSize());
           
        }

        @Override
            public boolean isCellEditable(int row, int column) { return false; }



        public int getRowHeight(int row) {
            return mainTable.getRowHeight();
        }
    }
}
